{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Users\\EDY\\anaconda3\\envs\\ps_torch\\lib\\site-packages\\tqdm\\auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "from PIL import Image\n",
    "import os\n",
    "from my_py_toolkit.file.file_toolkit import get_file_paths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_sub_dir(dir):\n",
    "    dir_par, sub_dirs = os.walk(dir).__next__()\n",
    "    sub_dirs = [f'{dir_par}/{sub_dir}' for v in sub_dir]\n",
    "    return sub_dirs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('.',\n",
       " ['.git', '.vscode', 'lib', 'log', 'model', 'output', 'test_files'],\n",
       " ['.gitignore',\n",
       "  'albumentations.ipynb',\n",
       "  'count_dir_filenums.py',\n",
       "  'data.ipynb',\n",
       "  'del_log.sh',\n",
       "  'fasterrcnn.ipynb',\n",
       "  'main.py',\n",
       "  'main_cls.py',\n",
       "  'main_sbi.py',\n",
       "  'predict_cls.ipynb',\n",
       "  'ps_detect.ipynb',\n",
       "  'ps_detect.py',\n",
       "  'README.en.md',\n",
       "  'README.md',\n",
       "  'test.ipynb',\n",
       "  'test_frcnn.ipynb',\n",
       "  'test_sbi.sh',\n",
       "  'train_sbi.sh',\n",
       "  'train_sbi_hj.sh'])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.walk('.').__next__()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(10):\n",
    "    print(np.log((i+1) * 0.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test(a):\n",
    "    def test_1(b):\n",
    "        return a + b\n",
    "    return test_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = test(1)\n",
    "t(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.tensor([1, 2])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2, 1, 1])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.concat([a, torch.ones_like(a)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAASsAAAErCAIAAAAJxjLjAAABG0lEQVR4nO3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHg0ZEgAB46tOfgAAAABJRU5ErkJggg==",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=299x299 at 0x1896FBAC588>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image.fromarray(np.uint8(np.random.random((299, 299, 3))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.asarray(torch.tensor([1]).size())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.17637123, 0.77826589, 0.88531482, 0.66035135, 0.30413144,\n",
       "       0.56181412, 0.04069118, 0.84244308, 0.00152423, 0.41635858,\n",
       "       0.15150638, 0.89179041, 0.50338661, 0.46221292, 0.78037643,\n",
       "       0.06297584, 0.36631865, 0.05905108, 0.8021339 , 0.52078441,\n",
       "       0.47911418, 0.56505458, 0.18656345, 0.4967939 , 0.99798193,\n",
       "       0.82515332, 0.54240416, 0.41950928, 0.48543637, 0.29393886,\n",
       "       0.61840719, 0.65355549, 0.78687506, 0.02272117, 0.00164566,\n",
       "       0.78091167, 0.83521186, 0.37548727, 0.59724759, 0.27015318,\n",
       "       0.16819474, 0.35307086, 0.83967499, 0.90244322, 0.23458941,\n",
       "       0.26482872, 0.07557116, 0.25694557, 0.99653798, 0.91759656,\n",
       "       0.68485428, 0.57995168, 0.56054874, 0.31722521, 0.30461795,\n",
       "       0.43161061, 0.05193791, 0.39294549, 0.64409992, 0.12118386,\n",
       "       0.76041671, 0.40809318, 0.84443179, 0.970361  , 0.55789013,\n",
       "       0.1020793 , 0.68467712, 0.54337149, 0.9599159 , 0.76988207,\n",
       "       0.23354509, 0.1444263 , 0.52303461, 0.68620247, 0.4225855 ,\n",
       "       0.79199584, 0.81903204, 0.9597399 , 0.73388588, 0.53431564,\n",
       "       0.34674093, 0.92268483, 0.36834226, 0.81953395, 0.37253328,\n",
       "       0.0376978 , 0.97875943, 0.36927811, 0.80730266, 0.65883843,\n",
       "       0.15728396, 0.01205134, 0.93158719, 0.5338321 , 0.9745788 ,\n",
       "       0.11640605, 0.40155128, 0.74352192, 0.9348653 , 0.86470392])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.random(100)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1,\n",
       "       1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0,\n",
       "       0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1,\n",
       "       1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1,\n",
       "       1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(0, 2, 100)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 0,  2,  4,  6,  9, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 26, 33,\n",
       "        34, 35, 36, 37, 38, 39, 40, 42, 45, 47, 48, 52, 54, 58, 59, 60, 63,\n",
       "        65, 66, 67, 69, 70, 71, 72, 73, 75, 78, 79, 86, 87, 88, 89, 90, 92,\n",
       "        96, 99], dtype=int64),)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx = np.where(b==1)\n",
    "idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27.56756158477468"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[idx].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_metric(prop, p_true):\n",
    "    min_score = prop[np.where(p_true==1)].min()\n",
    "    idx_larger_min = np.where(prop >= min_score)\n",
    "    label_predict_true = p_true[idx_larger_min]\n",
    "    return label_predict_true.sum() / label_predict_true.shape[0], min_score\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.5353535353535354, 0.0016456589283548873)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "get_metric(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.564335286617279"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 - 0.43566471338272095"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "01e1d2db26120223f72790c2bb0f23f31be95a9fbbaf387dcc46f865601cfb7f"
  },
  "kernelspec": {
   "display_name": "Python 3.6.5 ('ps_torch')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
